#pragma once

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include "comm.hpp"
#include <fcntl.h>
#include <unistd.h>

#define FILENAME "fifo"
#define PATH "."

#define ERR_EXIT(m) \
do \
{  \
    perror(0);\
    exit(EXIT_FAILURE);\
}while(0)
class NamedFifo
{
public:
    NamedFifo(const std::string &path, const std::string &name)
        : _path(path), _name(name)
    {
        _fifoname = _path + "/" + _name;
        umask(0);
        // 新建管道
        int n = mkfifo(_fifoname.c_str(), 0666);
        if (n < 0)
        {
            ERR_EXIT("mkfifo");
        }
        else
        {
            std::cout << "mkfifo success" << std::endl;
        }
    }
    ~NamedFifo()
    {
        // 删除管道文件
        int n = unlink(FILENAME);
        if (n == 0)
        {
            std::cout << "remove fifo success" << std::endl;
        }
        else
        {
            ERR_EXIT("remove fifo fail");
        }
    }

private:
    std::string _path;
    std::string _name;
    std::string _fifoname;
};

class FileOper
{
public:
    FileOper(const std::string &path, const std::string &name)
        : _path(path), _name(name), _fd(-1)
    {
        _fifoname = _path + "/" + _name;
    }
    void OpenForRead()
    {
        // read
        _fd = open(FILENAME, O_RDONLY);
        if (_fd < 0)
        {
            ERR_EXIT("open");
            return;
        }

        std::cout << "open fifo success" << std::endl;
    }
    void OpenForWrite()
    {
        // read
        _fd = open(FILENAME, O_WRONLY);
        if (_fd < 0)
        {
            ERR_EXIT("write");
            return;
        }

        std::cout << "open fifo success" << std::endl;
    }
    void Write()
    {
        // 写入操作
        std::string message;
        int cnt = 1;
        pid_t id = getpid();
        while (true)
        {
            std::cout << "Please Enter# ";
            std::getline(std::cin, message);
            message += (",message number:  " + std::to_string(cnt++) + ",[" + std::to_string(id) + "]");
            write(_fd, message.c_str(), message.size());
        }
    }
    void Read()
    {
        // 正常的read
    while (true)
    {
        char buffer[1024];
        int number = read(_fd, buffer, sizeof(buffer) - 1);
        if (number > 0)
        {
            buffer[number] = 0;
            std::cout << "Client say   " << buffer << std::endl;
        }
        else if(number == 0)
        {
            std::cout << "client quit? me too!" << number << std::endl;
            break;
        }
        else
        {
            std::cerr << "read error" << std::endl;   
            break;
        }
        
    }
    }
    void Close()
    {
        if (_fd > 0)
        {
            close(_fd);
        }
    }
    ~FileOper()
    {
        Close();
    }

private:
    std::string _path;
    std::string _name;
    std::string _fifoname;
    int _fd;
};